/**
 * @Title: FileUtils.java
 * @Package: com.jason.fingerprint.utils
 * @Descripton: TODO
 * @Author: Administrator zhangyujn1989ok@gmail.com
 * @Date: 2014年11月4日11:32:35
 * @Version: V1.0
 */
package com.jason.fingerprint.utils;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;

import java.io.File;
import java.io.IOException;

/**
 * @ClassName: FileUtils
 * @Description: 文件处理
 * @Author: Administrator zhangyujn1989ok@gmail.com
 * @Date: 2014年11月4日11:32:21
 */
public class FileUtils {

	private static final String TAG = "FileUtils";

	public static File getDiskCacheDir(Context context, String uniqueName) {
//		boolean flag1 = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
//		boolean flag2 = !isExternalStorageRemovable();
//		String path1 = getExternalCacheDir(context).getPath();
//		String path2 = context.getCacheDir().getPath();
//		Log.i(TAG, "----flag1:" + flag1);
//		Log.i(TAG, "----flag2:" + flag2);
//		Log.i(TAG, "----path1:" + path1);
//		Log.i(TAG, "----path2:" + path2);
		final String cachePath = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
				|| !isExternalStorageRemovable() ? getExternalCacheDir(context).getPath() : context.getCacheDir()
				.getPath();
		final File dir = new File(cachePath + File.separator + uniqueName);
		if (dir.exists()) {
			if (!dir.isDirectory()) {
				dir.delete();
			} else {
				return dir;
			}
		} else {
			dir.mkdirs();
		}
		return dir;
	}

	@TargetApi(8)
	public static File getExternalCacheDir(Context context) {
		return new File(Environment.getExternalStorageDirectory().getPath());
	}

	@TargetApi(9)
	public static boolean isExternalStorageRemovable() {
		if (hasGingerbread()) {
			return Environment.isExternalStorageRemovable();
		}
		return false;
	}

	/**
	 * 获得文件长度
	 * @param filePath
	 * @return
	 */
	public static int getFileLength(String filePath) {
		if (!isExist(filePath)) {
			return 0;
		}

		File file = new File(filePath);
		int length = (int) file.length();
		return length;
	}

	/**
	 * 是否存在
	 *
	 * @param filepath
	 * @return
	 */
	public static boolean isExist(String filepath) {
		File file = new File(filepath);
		return file.isFile();
	}

	/**
	 * 根据指定路径创建文件
	 * @param path
	 */
	public static File createFile(String path){
		File file = new File(path);
		if (!file.exists()){
			String directory = path.substring(0, path.lastIndexOf("/"));
			File dirFile = new File(directory);
			if (!dirFile.exists())
				dirFile.mkdirs();
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return file;
	}

	/***
	 * 删除制定文件
	 * @param path
	 */
	public static void deleteFile(String path) {
		File f = new File(path);
		if (f.exists()) {
			f.delete();
		}
	}

	/**
	 * 重命名文件,除文件名外文件路径必须保持一致
	 * @param oldPath
	 * @param newPath
	 */
	public static void renameTo(String oldPath, String newPath){
		File oldFile = new File(oldPath);
		File newFile = new File(newPath);
		oldFile.renameTo(newFile);
	}

	public static long getAvailaleSize(){
		if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
				|| !isExternalStorageRemovable()){
			File path = Environment.getExternalStorageDirectory(); //取得sdcard文件路径
			StatFs stat = new StatFs(path.getPath());
			long blockSize = stat.getBlockSize();
			long availableBlocks = stat.getAvailableBlocks();
			return availableBlocks * blockSize;
		} else{
			return 0;
		}
	}

	public static boolean hasFroyo() {
		return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO;
	}

	public static boolean hasGingerbread() {
		return Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD;
	}

	public static boolean hasHoneycomb() {
		return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
	}

	public static boolean hasHoneycombMR1() {
		return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1;
	}

	public static boolean hasJellyBean() {
		return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
	}

}
